//
//  ToolViewViewController.m
//  IWaneHome
//
//  Created by hwd on 印度历1938/3/22.
//  Copyright © 印度历1938年 hwd_zlg. All rights reserved.
//

#import "ToolViewVC.h"

@implementation ToolViewVC

//导航条设置
+(void)DaoHangtiaoColor:(UIViewController *)selfControV
{
    selfControV.navigationController.navigationBarHidden = NO;
    selfControV.extendedLayoutIncludesOpaqueBars = YES;
    selfControV.navigationController.navigationBar.translucent = NO;
    
    //去掉back字
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];
    //改变导航条背景颜色
    [selfControV.navigationController.navigationBar setBarTintColor:mainColor];
    //改变导航条标题栏字体颜色
    [selfControV.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#333333"]}];
    //改变导航条返回键颜色
    selfControV.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}
//获得字符串的宽度
+(CGFloat)getWidthFromString:(NSString *)str Font:(float)font
{
    CGRect rect = [str boundingRectWithSize:CGSizeMake(220, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName, nil] context:nil];
    return rect.size.width;
}
//弹窗
+(void)addAlterViewController:(NSString *)msg Controller:(UIViewController *)controller
{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:msg preferredStyle:  UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [controller presentViewController:alert animated:true completion:nil];
}
//pop弹窗
+(void)addPopAlterViewController:(NSString *)msg Controller:(UIViewController *)controller
{
    [IQKeyboardManager sharedManager].enable = NO;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [controller.navigationController popViewControllerAnimated:YES];
    }]];
    //弹出提示框；
    [controller presentViewController:alert animated:true completion:nil];
}
//push返回 
+(void)addPresentAlterViewController:(NSString *)msg Controller:(UIViewController *)controller
{
    [IQKeyboardManager sharedManager].enable = NO;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [controller dismissViewControllerAnimated:YES completion:nil];
    }]];
    //弹出提示框；
    [controller presentViewController:alert animated:true completion:nil];
}
//Key弹窗
+(void)addKeyAlterViewController:(NSString *)msg Controller:(UIViewController *)controller
{
    [IQKeyboardManager sharedManager].enable = NO;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:msg preferredStyle:  UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //点击按钮的响应事件；
        [IQKeyboardManager sharedManager].enable = YES;
    }]];
    //弹出提示框
    [controller presentViewController:alert animated:true completion:nil];
}
+(UILabel *)addLineLab:(CGRect)cg
{
    UILabel *label = [[UILabel alloc]initWithFrame:cg];
    label.backgroundColor = LineColor;
    return label;
}
+(UILabel *)addCellLineLab:(CGRect)cg
{
    UILabel *label = [[UILabel alloc]initWithFrame:cg];
    label.backgroundColor = LineColor;
    return label;
}
+ (float) heightForTextViewWithText: (NSString *) strText{
    CGSize constraint = CGSizeMake(SCREENW-50*screenW_6, CGFLOAT_MAX);
    CGRect size = [strText boundingRectWithSize:constraint
                                        options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                     attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]}
                                        context:nil];
    float textHeight = size.size.height + 30.0;
    
    return textHeight;
}
+(BOOL)isNotNullStr:(id)DataStr
{
    if ([DataStr isEqual:@""] || [DataStr isEqual:@" "] || [DataStr isKindOfClass:[NSNull class]] || [DataStr isEqual:@"<null>"] || [DataStr isEqual:@"(null)"] || DataStr == NULL || DataStr == nil) {
        return NO;
    }
    return YES;
}
+(NSString *)ChangeData:(NSString *)str IntType:(NSString *)IntType OutType:(NSString *)outType
{
    if (![self isNotNullStr:str]) {
        return @"";
    }
    NSDateFormatter *dateFormatterYear = [[NSDateFormatter alloc] init];
    [dateFormatterYear setDateFormat:IntType];
    NSDate *time = [dateFormatterYear dateFromString:str];
    [dateFormatterYear setDateFormat:outType];
    NSString *YearDaySrt = [dateFormatterYear stringFromDate:time];
    
    return YearDaySrt;
}
+(NSString *)ChangeTimeYearMD:(NSDate *)date Type:(NSString *)outType//返回年月日
{
    if (![self isNotNullStr:date]) {
        return @"";
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:outType];
    NSString *nowTime = [formatter stringFromDate:date];
    return nowTime;
}
+(id)getNotNullStr:(id)DataStr
{
    if ([DataStr isEqual:@""] || [DataStr isEqual:@" "] || [DataStr isKindOfClass:[NSNull class]] || [DataStr isEqual:@"<null>"] || DataStr == NULL || !DataStr || [DataStr isEqual:@"(null)"] || DataStr == nil ||  [DataStr isEqual:@"null null"]) {
        return @" ";
    }
    return DataStr;
}
//获得非空数字
+(id)getNotNullReturnNumberStr:(id)DataStr
{
    if ([DataStr isEqual:@""] || [DataStr isEqual:@" "] || [DataStr isKindOfClass:[NSNull class]] || [DataStr isEqual:@"<null>"] || DataStr == NULL || !DataStr || [DataStr isEqual:@"(null)"] || DataStr == nil) {
        return @"0";
    }
    return DataStr;
}
//获得非空字典
+(id)getNotDic:(id)DataStr
{
    if ([DataStr isEqual:@""] || [DataStr isKindOfClass:[NSNull class]] || [DataStr isEqual:@"<null>"] || DataStr == NULL || !DataStr || [DataStr isEqual:@"(null)"] || DataStr == nil ) {
        return @{};
    }
    return DataStr;
}
//去除空字符串
+(id)getNotKongGeStr:(id)DataStr
{
    DataStr = [DataStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    return DataStr;
}
//判空
+(BOOL)isNull:(id)DataStr
{
    DataStr = [DataStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([DataStr isEqual:@""] || [DataStr isKindOfClass:[NSNull class]] || [DataStr isEqual:@"<null>"] || DataStr == NULL || !DataStr || [DataStr isEqual:@"(null)"] || DataStr == nil ) {
        return YES;
    }
    return NO;
}
//时间Dete
+ (NSDate *)getTimeDate:(NSString *)Time IntType:(NSString *)type
{
    NSDateFormatter *date=[[NSDateFormatter alloc] init];
    [date setDateFormat:type];
    NSDate *d1 = [date dateFromString:[NSString stringWithFormat:@"%@",Time]];
    NSTimeInterval timeInterval=[d1 timeIntervalSince1970]*1;
    NSDate *dateTime = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    return dateTime;
}

+ (NSString *)getTimeStr:(NSString *)time
{
    float timeFloat = [time floatValue];
    NSString *timeStr;
    if (timeFloat>3600) {
        timeStr = [NSString stringWithFormat:@"%.1f小时",timeFloat/3600];
    }else if (timeFloat>60)
    {
        timeStr = [NSString stringWithFormat:@"%.1f分钟",timeFloat/60];
    }else
    {
        timeStr = [NSString stringWithFormat:@"%.0f秒",timeFloat];
    }
    
    return timeStr;
}

//将对象转换成json
+ (NSString *)beJsonStrWithObj:(id)obj
{
    NSData *data=[NSJSONSerialization dataWithJSONObject:obj options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"    " withString:@""];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"(\\n)"
                                                 withString:@"" options:NSRegularExpressionSearch
                                                      range:NSMakeRange(0, [jsonStr length])];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"(\")"
                                                 withString:@"\'" options:NSRegularExpressionSearch
                                                      range:NSMakeRange(0, [jsonStr length])];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"(\\)"
                                                 withString:@"" options:NSRegularExpressionSearch
                                                      range:NSMakeRange(0, [jsonStr length])];
    return jsonStr;
}


+(NSDecimalNumber *)getCalculateFirstNumberStr:(id)firstNumberStr SecondNumberStr:(id)secondNumberStr TypeNumber:(NSInteger)typerNumber
{
    NSString *firstNumber = [NSString stringWithFormat:@"%@",firstNumberStr];
    NSDecimalNumber *firstDecNumber = [NSDecimalNumber decimalNumberWithString:firstNumber];
    
    NSString *secondNumber = [NSString stringWithFormat:@"%@",secondNumberStr];
    NSDecimalNumber *secondDecNumber = [NSDecimalNumber decimalNumberWithString:secondNumber];
    
    NSDecimalNumber  *resultDecNumber;
    
    if (typerNumber == 0) {//加
        resultDecNumber  =  [firstDecNumber decimalNumberByAdding:secondDecNumber];
    }else if (typerNumber == 1)//减
    {
        resultDecNumber  =  [firstDecNumber decimalNumberBySubtracting:secondDecNumber];
        
    }else if (typerNumber == 2)//乘
    {
        resultDecNumber  =  [firstDecNumber decimalNumberByMultiplyingBy:secondDecNumber];
        
    }else if (typerNumber == 3)//除
    {
        resultDecNumber  =  [firstDecNumber decimalNumberByDividingBy:secondDecNumber];
    }
    return resultDecNumber;
}
//更新
+(void)addGengXin
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSArray *neiArr;
    if([appVersion rangeOfString:@"."].location !=NSNotFound)//是否含有.
    {
        neiArr = [appVersion componentsSeparatedByString:@"."];
    }else//不含有
    {
        neiArr = [[NSArray alloc]initWithObjects:appVersion,@"0",nil];
    }
    //appType 类型(0=商家版,1=用户版，2=师傅版)
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:[NSString stringWithFormat:@"/jeesite/appInterface/userInfoInterface/versionDetails"] Dic:@{@"models":@"1",@"appType":@"0"} completion:^(NSDictionary *responseDic) {
            if ([responseDic[@"success"] boolValue]) {//请求成功
                NSString *version = responseDic[@"version"][@"varsionCode"];
                version = [version stringByReplacingOccurrencesOfString:@"v" withString:@""];
                if([version rangeOfString:@"."].location !=NSNotFound)//含有点
                {
                    NSArray *arr = [version componentsSeparatedByString:@"."];
                    if ([neiArr[0] intValue]<[arr[0] intValue]) {//第一个数小于跟新
                        [self goLoadAppUrl:responseDic];
                        return;
                    }
                    else if ([neiArr[0] intValue] ==[arr[0] intValue]) {//第一个数小于相等
                        
                        if ([neiArr[1] intValue]<[arr[1] intValue]) {//比较第二个数
                            [self goLoadAppUrl:responseDic];
                            return;
                            
                        }else if ([neiArr[1] intValue]==[arr[1] intValue] && neiArr.count>=3 && arr.count>=3) {
                            
                            if ([neiArr[2] intValue]< [arr[2] intValue]) {//前两位相等 第三个数小于跟新
                                [self goLoadAppUrl:responseDic];
                                return;
                            }
                            
                        } else if ([neiArr[1] intValue]==[arr[1] intValue] && arr.count>=3)
                        {
                            
                            if ([arr[2] intValue]>0)//前两位相等 第三位>0 更新
                            {
                                [self goLoadAppUrl:responseDic];
                                return;
                            }
                        }
                    }
                }else//不包含远点 直接比较
                {
                    if([appVersion integerValue]<[version integerValue])
                    {
                        [self goLoadAppUrl:responseDic];
                        return;
                    }
                }
            }
        }];
    });
}
+(void)goLoadAppUrl:(NSDictionary *)genXinDic//去更新
{
    NSString *url = genXinDic[@"version"][@"urls"];
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"有新版本 请更新" preferredStyle:  UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if([url rangeOfString:@"http"].location !=NSNotFound)
            {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
            }
            else
            {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@",url]]];
            }
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
        //弹出提示框；
        [[TomamaUI getTopVC] presentViewController:alert animated:true completion:nil];
    });
}


+(void)creatTopKeyBoardAlertWithTarget:(UIViewController *)vc message:(NSString *)message timeInterval:(CGFloat)timeInterval
{
    
    if ([message isEqual:[NSNull null]]) {//!message || [message isEqual:[NSNull null]]
        message = @"请求超时";
    }
    UILabel *label = [TomamaUI createLabel:CGRectMake(vc.view.frame.size.width/2 - ([TomamaUI getStringWeithtWith:message andHeigth:44*screenW_6 andFont:[UIFont systemFontOfSize:13*screenW_6]] + 20*screenW_6)/2, vc.view.frame.size.height/2 - 100*screenW_6, [TomamaUI getStringWeithtWith:message andHeigth:44*screenW_6 andFont:[UIFont systemFontOfSize:13*screenW_6]] + 20*screenW_6, 44*screenW_6) textAligment:1 font:[UIFont systemFontOfSize:13*screenW_6] textColor:[UIColor whiteColor] text:message];
    if (label.frame.size.width > SCREENW*2/3) {
        label.frame = CGRectMake(vc.view.frame.size.width/2 - SCREENW*1/3, vc.view.frame.size.height/2 - ([TomamaUI getStringHeightWith:message andWeith:SCREENW*2/3 andFont:[UIFont systemFontOfSize:13*screenW_6]] + 20*screenW_6)/2, SCREENW*2/3, [TomamaUI getStringHeightWith:message andWeith:SCREENW*2/3 andFont:[UIFont systemFontOfSize:13*screenW_6]] + 20*screenW_6);
    }
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor blackColor];
    label.layer.masksToBounds = YES;
    label.layer.cornerRadius = 3;
    label.alpha = 0.8;
    [vc.view addSubview:label];
    [UIView animateWithDuration:timeInterval animations:^{
        label.alpha = 0;
    } completion:^(BOOL finished) {
        [label removeFromSuperview];
    }];
}
@end
